# Maintainer: Biswapriyo Nath <nathbappai@gmail.com>

_realname=supertux
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.6.3
pkgrel=2
pkgdesc="A classic 2D jump'n'run sidescroller game (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/supertux/supertux'
license=('spdx:GPL-3.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-SDL2"
         "${MINGW_PACKAGE_PREFIX}-SDL2_image"
         "${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-fribidi"
         "${MINGW_PACKAGE_PREFIX}-glew"
         "${MINGW_PACKAGE_PREFIX}-harfbuzz"
         "${MINGW_PACKAGE_PREFIX}-icu"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libraqm"
         "${MINGW_PACKAGE_PREFIX}-libvorbis"
         "${MINGW_PACKAGE_PREFIX}-openal"
         "${MINGW_PACKAGE_PREFIX}-physfs")
makedepends=("${MINGW_PACKAGE_PREFIX}-boost"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-glm"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("https://github.com/SuperTux/supertux/releases/download/v${pkgver}/SuperTux-v${pkgver}-Source.tar.gz"
        0001-cmake-disable-dependencies-paths.patch
        0002-cmake-fix-linking-order.patch
        0003-include-missing-header.patch
        0004-cmake-fix-iconv-check.patch)
noextract=("SuperTux-v${pkgver}-Source.tar.gz")
sha256sums=('f7940e6009c40226eb34ebab8ffb0e3a894892d891a07b35d0e5762dd41c79f6'
            '9577cbee1c9affa6147ebb20345cef542bd340ea278074099b197f123f4e1ab9'
            'f869efc73f9d6bf5694257845e1f4b5c74831a798ec638126e7aca7c01838832'
            '9259b303a62dbe74b1a6b5224d8860496c5a501fa5aa7658aa44dc92da30695f'
            '7d74c3623f87e969352b23faf7f14f5c3082254496ef7986ab96080f7e3517ec')

prepare() {
  tar -xf "SuperTux-v${pkgver}-Source.tar.gz" || true
  cd "${srcdir}"/SuperTux-v${pkgver}-Source

  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    sed -e "s|-funit-at-a-time||g" -i CMakeLists.txt
  fi

  patch -p1 -i "${srcdir}"/0001-cmake-disable-dependencies-paths.patch
  patch -p1 -i "${srcdir}"/0002-cmake-fix-linking-order.patch
  patch -p1 -i "${srcdir}"/0003-include-missing-header.patch
  patch -p1 -i "${srcdir}"/0004-cmake-fix-iconv-check.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DENABLE_DISCORD=ON \
      $([[ ${CARCH} == aarch64 ]] && echo "-DENABLE_OPENGL=OFF") \
      ../SuperTux-v${pkgver}-Source

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  rm -v "${pkgdir}${MINGW_PREFIX}"/bin/*.a
  rm -v "${pkgdir}${MINGW_PREFIX}"/lib/*.a

  install -Dm644 "${srcdir}/SuperTux-v${pkgver}-Source/LICENSE.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.txt"
}
